package com.example.sherlockzhong.plush_v2.Subscriptions;

/*
 * Created by DosonLeungs on 2014/8/23.
 */

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.example.sherlockzhong.plush_v2.CardBeans.OrgBean;
import com.example.sherlockzhong.plush_v2.CardIni.OrganizationIni;
import com.example.sherlockzhong.plush_v2.Constants.SystemConstant;
import com.example.sherlockzhong.plush_v2.Models.PushModel;
import com.example.sherlockzhong.plush_v2.R;
import com.example.sherlockzhong.plush_v2.Utils.DialogUtil;
import com.example.sherlockzhong.plush_v2.Utils.NetworkUtils;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;

public class OrganizationActivity extends Activity {
    public ArrayList<ImageView> imageList = new ArrayList<ImageView>();
    public ArrayList<HashMap<String, Object>> bitmapList = new ArrayList<HashMap<String, Object>>();
    OrgBean organizationbean = new OrgBean();
    OrganizationIni OM;
    JSONArray postersArray;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            stopReflashDialog();
            if (msg.what == 0x123) {
                if (postersArray != null) {
                    OM = new OrganizationIni(OrganizationActivity.this, OrganizationActivity.this);
                    OM.init(postersArray.length(), postersArray, organizationbean);
                } else {
                    OM = new OrganizationIni(OrganizationActivity.this, OrganizationActivity.this);
                    OM.init(0, null, organizationbean);
                }
            }
        }
    };
    private ProgressDialog pd;
    private String result = "";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subscription_organization_list);
        showReflashDialog("加载社团中...");
        new Thread() {
            @Override
            public void run() {
                postersArray = PushModel.retrievePosters("subscribe");
                handler.sendEmptyMessage(0x123);
            }
        }.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        this.result = intent.getExtras().getString("result");//得到新Activity 关闭后返回的数据
    }

    @Override
    public void onRestart() {
        super.onRestart();
        organizationbean = new OrgBean();
        if (result.equals("")) {
            OM.reverse();
            if (SystemConstant.updateOrgs) {
                showReflashDialog("更新中...");
                new Thread() {
                    @Override
                    public void run() {
                        if (NetworkUtils.getAvailableNetWorkInfo(OrganizationActivity.this) == null) {
                            postersArray = PushModel.retrievePosters("subscribed");
                        } else {
                            postersArray = PushModel.listSubscribedPosters();
                        }
                        handler.sendEmptyMessage(0x123);
                    }
                }.start();
            } else {
                handler.sendEmptyMessage(0x123);
            }
        } else {
            result = "";
        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    /**
     * 显示刷新的时候的等待进度条
     */
    public void showReflashDialog(String msg) {
        this.pd = new ProgressDialog(this);
        this.pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        this.pd.setCancelable(true);
        this.pd.setMessage(msg);
        this.pd.show();
    }

    /**
     * 结束刷新的时候的等待进度条
     */


    public void stopReflashDialog() {
        if (this.pd != null) {
            pd.dismiss();
        }
    }

    public void onDestory() {
        super.onDestroy();
        stopReflashDialog();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            stopReflashDialog();
            DialogUtil.showDialog(this, "确认返回?");
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}